<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <title>部门列表</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css">
    <link rel="stylesheet" href="../../../admin/css/dimples.css">
    <link rel="icon" href="../../../admin/images/favicon.ico" type="image/x-icon"/>
</head>

<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">部门名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="deptName" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="dept-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="dept-table" lay-filter="dept-table"></table>
    </div>
</div>

<script type="text/html" id="dept-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add"
            {{ checkPerms('dept:add') }}>
    <i class="layui-icon layui-icon-add-1"></i>
    新增
    </button>
    <button class="pear-btn pear-btn-md" lay-event="batchRemove" {{ checkPerms('dept:delete') }}>
    <i class="layui-icon layui-icon-delete"></i>
    删除
    </button>
</script>

<script type="text/html" id="dept-bar">
    <span {{noPerms('dept:delete','dept:edit')}} >
    <span class="layui-badge-dot dimples-bg-orange"></span>
    无权限
    </span>

    <a lay-event="edit" {{checkPerms('dept:edit')}}>
    <i class="layui-icon layui-icon-set dimples-edit-area dimples-blue"></i>
    </a>
    </a>
    <a lay-event="remove" {{checkPerms('dept:delete')}}>
    <i class="layui-icon dimples-edit-area dimples-red">&#xe7f9;</i>
    </a>
</script>

<script type="text/html" id="dept-status">
    <input type="checkbox" name="dept-status" value="{{d.deptId}}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="dept-status" {{ d.status== '1' ? 'checked' : '' }} />
</script>

</body>

<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script src="../../../component/pear/dimples.js"></script>

<script>
    layui.use(['table', 'form', 'jquery', 'treetable', 'popup', 'open', 'ajax', 'loading', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let treetable = layui.treetable;
        let popup = layui.popup;
        let open = layui.open;
        let ajax = layui.ajax;
        let loading = layui.loading;
        let common = layui.common;

        let MODULE_PATH = "/system/dept";

        window.render = function () {
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'deptId',
                treePidName: 'parentId',
                skin: 'line',
                method: 'get',
                treeDefaultClose: true,
                toolbar: '#dept-toolbar',
                elem: '#dept-table',
                url: MODULE_PATH,
                page: false,
                cols: [
                    [
                        {type: 'checkbox'},
                        {field: 'deptName', minWidth: 200, title: '部门名称'},
                        {field: 'leaderName', title: '负责人'},
                        {field: 'mobile', title: '联系方式'},
                        {field: 'email', title: '邮箱'},
                        {field: 'status', title: '状态', templet: '#dept-status'},
                        {field: 'sort', title: '排序'},
                        {field: 'address', title: '详细地址'},
                        {field: 'deptId', title: 'ID', hide: true},
                        {title: '操作', templet: '#dept-bar', width: 120, align: 'center'}
                    ]
                ]
            });
        }

        form.on('submit(dept-query)', function (data) {
            var keyword = data.field.deptName;
            var $tds = $('#dept-table').next('.treeTable').find('.layui-table-body tbody tr td');
            if (!keyword) {
                $tds.css('background-color', 'transparent');
                layer.msg("请输入关键字", {icon: 5});
                return;
            }
            var searchCount = 0;
            $tds.each(function () {
                $(this).css('background-color', 'transparent');
                if ($(this).text().indexOf(keyword) >= 0) {
                    $(this).css('background-color', 'rgba(250,230,160,0.5)');
                    if (searchCount == 0) {
                        $('body,html').stop(true);
                        $('body,html').animate({scrollTop: $(this).offset().top - 150}, 500);
                    }
                    searchCount++;
                }
            });
            if (searchCount == 0) {
                layer.msg("没有匹配结果", {icon: 5});
            } else {
                treetable.expandAll('#dept-table');
            }
            return false;
        });

        render();

        table.on('tool(dept-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        })

        table.on('toolbar(dept-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('switch(dept-status)', function (obj) {
            let operate;
            if (obj.elem.checked) {
                operate = "1";
            } else {
                operate = "0";
            }
            loading.Load(5, '');
            ajax.send('/system/dept/status', {
                data: JSON.stringify({
                    deptId: this.value,
                    status: operate
                }),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
            }, function (result) {
                popup.success(result.msg);
            });
        });

        window.add = function () {
            open.iframe('新增', 'add.html');
        }

        window.edit = function (obj) {
            open.iframe('修改', 'edit.html?deptId=' + obj.data['deptId']);
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该部门', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                loading.Load(5, '');
                ajax.send(MODULE_PATH + "/" + obj.data['deptId'], {
                    dataType: 'json',
                    type: 'DELETE',
                }, function (result) {
                    popup.success(result.msg, function () {
                        obj.del();
                    });
                });
            });
        }

        window.batchRemove = function (obj) {
            let ids = common.checkField(obj, 'deptId');
            if (common.isEmpty(ids)) {
                popup.warning("未选中数据");
                return false;
            }
            layer.confirm('确定要删除选中部门', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                loading.Load(5, '');
                ajax.send(MODULE_PATH + '/batch/' + ids, {
                    type: 'DELETE',
                }, function (result) {
                    popup.success(result.msg, function () {
                        table.reload('dept-table');
                    });
                });
            });
        }
    })
</script>

</html>